#save all keys
import pythoncom, pyHook, time, ctypes, pickle, sys

Key2ID = {}
ID2Key = {}

RUNTIME = 120

start = time.time()
sys.stdout = open('stdout','w')

def OnKeyDown(event):

    if not(event.KeyID in list(ID2Key)):
        print event.Key

    ID2Key[event.KeyID] = event.Key
    Key2ID[event.Key] = event.KeyID

    

    if time.time() - start > RUNTIME:
        #exit program
        ctypes.windll.user32.PostQuitMessage(0)

 
    return True
 
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyDown

# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()


#pickle data and save
pickleKey2ID = pickle.dumps(Key2ID)
fKey2ID = open('Key2ID', 'w')
fKey2ID.write(pickleKey2ID)
fKey2ID.close()

pickleID2Key = pickle.dumps(ID2Key)
fID2Key = open('ID2Key', 'w')
fID2Key.write(pickleID2Key)
fID2Key.close()
